//
//  ShareView.m
//  JPWords
//
//  Created by 梁淑婷 on 2024/5/28.
//

#import "ShareView.h"

@interface ShareView()

@property(nonatomic,strong)UIView *bgView;

@end

@implementation ShareView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        self.currentAvailableWidth = frame.size.width;
        self.currentAvailableHeight = frame.size.height;
        [self createUI];
    }
    return self;
}

-(void)createUI{
    
    self.backgroundColor = UIColorHex_(0x000000,0.4);
    
    self.bgView = [[UIView alloc]initWithFrame:CGRectMake(0,self.currentAvailableHeight,self.currentAvailableWidth,196)];
    self.bgView.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.bgView];
    [self.bgView addRoundedCorners:UIRectCornerTopLeft|UIRectCornerTopRight withRadii:CGSizeMake(10,10)];
    
    __weak typeof(self)self_weak_ = self;
    UILabel *titleLabel = [[UILabel alloc]init];
    titleLabel.font = JPUIFontPingFangSCMedium(17);
    titleLabel.textColor = KFontBlackColor;
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.text = @"分享至";
    [self.bgView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(@24);
        make.top.mas_equalTo(@14);
        make.width.mas_equalTo(@100);
        make.centerX.mas_equalTo(self_weak_.bgView);
    }];
    
    UIButton *closeBtn = [[UIButton alloc]init];
    [closeBtn setImage:[UIImage imageNamed:@"share_close"] forState:UIControlStateNormal];
    [closeBtn addTarget:self action:@selector(closeBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.bgView addSubview:closeBtn];
    [closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(@40);
        make.right.mas_equalTo(@-6);
        make.centerY.mas_equalTo(titleLabel);
    }];
    
    NSArray *arr = @[@"微信好友",@"朋友圈"];
    NSArray *imageArr = @[@"share_wechat",@"share_wechat_cycle"];
    CGFloat jiange = (self.currentAvailableWidth-68*2)/3.0;
    CGFloat left = jiange;
    for(NSInteger i = 0;i < arr.count;i ++){
        UIView *itemView = [[UIView alloc]init];
        itemView.tag = 1000+i;
        [self.bgView addSubview:itemView];
        [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(@64);
            make.height.mas_equalTo(@78);
            make.width.mas_equalTo(@68);
            make.left.mas_equalTo(left);
        }];
        left = left+68+jiange;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithActionBlock:^(id  _Nonnull sender) {
            if(self_weak_.shareBlock){
                self_weak_.shareBlock(i);
            }
        }];
        [itemView addGestureRecognizer:tap];
        UIImageView *logoImageView = [[UIImageView alloc]init];
        logoImageView.image = [UIImage imageNamed:imageArr[i]];
        logoImageView.userInteractionEnabled = YES;
        [itemView addSubview:logoImageView];
        [logoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(@0);
            make.width.height.mas_equalTo(@48);
            make.centerX.mas_equalTo(itemView);
        }];
        UILabel *titleLabel = [[UILabel alloc]init];
        titleLabel.font = JPUIFontPingFangSCRegular(13);
        titleLabel.textColor = KFontBlackColor;
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.text = arr[i];
        titleLabel.userInteractionEnabled = YES;
        [itemView addSubview:titleLabel];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(logoImageView.mas_bottom).offset(6);
            make.height.mas_equalTo(@18);
            make.width.mas_equalTo(@60);
            make.centerX.mas_equalTo(itemView);
        }];
    }
    
}

-(void)refreshWidth{
    if(self.isShow == YES){
        self.bgView.frame = CGRectMake(0,self.currentAvailableHeight-196,self.currentAvailableWidth,196);
    }else{
        self.bgView.frame = CGRectMake(0,self.currentAvailableHeight,self.currentAvailableWidth,196);
    }
    [self.bgView addRoundedCorners:UIRectCornerTopLeft|UIRectCornerTopRight withRadii:CGSizeMake(10,10)];
    CGFloat jiange = (self.currentAvailableWidth-68*2)/3.0;
    CGFloat left = jiange;
    for(NSInteger i = 0;i < 2;i++){
        UIView *itemView = [self.bgView viewWithTag:1000+i];
        [itemView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(left);
        }];
        left = left+68+jiange;
    }
}

#pragma mark - 关闭
-(void)closeBtnClick{
    [self hidden];
}


-(void)show{
    self.isShow = YES;
    __weak typeof(self)self_weak_ = self;
    [UIView animateWithDuration:0.2 animations:^{
        self_weak_.bgView.frame = CGRectMake(0,self_weak_.currentAvailableHeight-196,self_weak_.currentAvailableWidth,196);
    } completion:^(BOOL finished) {

    }];
}

-(void)hidden{
    self.isShow = NO;
    __weak typeof(self)self_weak_ = self;
    [UIView animateWithDuration:0.2 animations:^{
        self_weak_.bgView.frame = CGRectMake(0,self_weak_.currentAvailableHeight,self_weak_.currentAvailableWidth,196);
    } completion:^(BOOL finished) {
        [self_weak_ removeFromSuperview];
    }];
}

@end
